windows下用Eclipse连接大数据环境得hbase

1、解压hbase安装包

2、将大数据环境得hadoop安装包拷贝到windows(这里以d:/hadoop为例)

3、打开C:\Windows\System32\drivers\etc目录下的hosts并添加如下代码

127.0.0.1 localhost
192.168.48.134 master
192.168.48.133 slaver

注:这里你配置了几台服务器就写几台,这里我只配置192.168.48.134 master和192.168.48.133 slaver两台

4、使用Eclipse创建一个java工程,引进第一步骤中解压Hbase文件夹下的lib里面的全部jar

5、最好也在src下面创建一个文件夹,并把集群环境中hbase的hbase-site.xml放入文件文件夹中,并添加到class path中(其实新版本的都可省略这步了,因为我的hadoop是2.7.3,hbase用的是1.2.4算是比较高度版本了)

6、在步骤二的文件夹的bin文件夹下是否有winutils.exe文件,如果没有的话,就下载一个放在该目录

7、示例代码:

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;

public class TestHbase {

  private static final String TABLE_NAME = "test";
  private static final String CF_DEFAULT = "info";

  public static void createOrOverwrite(Admin admin, HTableDescriptor table) throws IOException {
    if (admin.tableExists(table.getTableName())) {
      admin.disableTable(table.getTableName());
      admin.deleteTable(table.getTableName());
    }
    admin.createTable(table);
  }

  public static void createSchemaTables(Configuration config) throws IOException {
    try (Connection connection = ConnectionFactory.createConnection(config);
         Admin admin = connection.getAdmin()) {

      HTableDescriptor table = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
      table.addFamily(new HColumnDescriptor(CF_DEFAULT).setCompressionType(Algorithm.NONE));

      System.out.print("Creating table. ");
      createOrOverwrite(admin, table);
      System.out.println(" Done.");
    }
  }

  public static void main(String[] args) throws IOException {  
        Configuration config = HBaseConfiguration.create();  
  
        /* 
        * 默认会从项目编译后的calss文件的根目录寻找文件,可以不指定 
        * 添加资源文件有以下几种方式: 
        * */  
        /** 
         * config.addResource(input); 
         */  
        //InputStream input = HBaseOper.class.getResourceAsStream("/core-site.xml");  
        //config.addResource(input);  
  
        /** 
         * 官网给出的例子程序使用的是这种方法 
         * config.addResource(Path); 
         * Path可以为绝对路径,例如:D:\\Program Files\\hbase\\conf\\hbase-site.xml 
         */  
      //这里就是步骤二从集群环境拷过来的hadoop放在window上的路径,注意都是用的绝对路径 System.setProperty(
"hadoop.home.dir", "D:\\Users\\zml\\Eclipse\\hadoop-2.7.3"); //Add any necessary configuration files (hbase-site.xml, core-site.xml)
    //这些文件都是从集群环境上拷过来的
config.addResource(new Path("D:\\Users\\zml\\Eclipse\\workspace\\HbaseDemo\\hbase_conf\\hbase-site.xml")); config.addResource(new Path("D:\\Users\\zml\\Eclipse\\workspace\\HbaseDemo\\hbase_conf\\core-site.xml")); createSchemaTables(config); //modifySchema(config); } }

 

posted @ 2017-03-16 15:54  zouminglan  阅读(2565)  评论(0编辑  收藏  举报